package com.cq.hd.admin.controller;

import com.cq.hd.admin.annotation.RequiresPermissionsDesc;
import com.cq.hd.admin.po.TbBusinessAdminUserPo;
import com.cq.hd.common.enums.ResponseEnums;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.Throw;
import com.cq.hd.common.utils.IpUtil;
import com.cq.hd.member.api.TbMemberCardApi;
import com.cq.hd.member.api.dto.*;
import com.cq.hd.member.api.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "商家后台商家次卡相关接口")
@Slf4j
@RestController
@RequestMapping("/admin/business/memberCard")
public class AdminBusinessMemberCardController extends BaseController {

    @Autowired
    private TbMemberCardApi memberCardApi;

    @ApiOperation(value = "次卡购买列表")
    @ApiResponse(response = AppMemberCardPageVo.class, code = 200, message = "接口返回对象参数")
    @RequiresPermissions("adminBusiness:adminMemberCard:list")
    @RequiresPermissionsDesc(menu = {"商家后台", "账户中心-次卡配置"}, button = "查询")
    @GetMapping("/page")
    public Result<Page<AppMemberCardPageVo>> pageAppMemberCard(AppMemberCardPageDto appMemberCardPageDto) {
        return memberCardApi.pageAppMemberCard(appMemberCardPageDto);
    }

    @ApiOperation(value = "购买次卡网银支付")
    @ApiResponse(response = BankPayRechargeVo.class, code = 200, message = "接口返回对象参数")
    @RequiresPermissions("adminBusiness:adminMemberCard:pay")
    @RequiresPermissionsDesc(menu = {"商家后台", "账户中心-次卡配置"}, button = "支付")
    @PostMapping("/pay")
    public Result<BankPayRechargeVo> pay(@RequestBody AdminBusinessMemberCardPayDto adminBusinessMemberCardPayDto, HttpServletRequest request) {
        TbBusinessAdminUserPo businessAdminUserPo = getBusinessAdminUser();
        if (businessAdminUserPo == null) {
            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
        }

        Long businessId = businessAdminUserPo.getBusinessId();
        Long appUserId = businessAdminUserPo.getAppUserId();
        if (businessId == null || businessId <= 0 || appUserId == null || appUserId <= 0) {
            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
        }

        adminBusinessMemberCardPayDto.setUserId(businessAdminUserPo.getAppUserId());
        adminBusinessMemberCardPayDto.setBusinessId(businessId);
        adminBusinessMemberCardPayDto.setRequestIp(IpUtil.getIpAddr(request));

        return memberCardApi.bankPayAppMemberCardOrder(adminBusinessMemberCardPayDto);
    }

//    @ApiOperation(value = "查单")
//    @ApiResponse(response = BankPayQueryVo.class, code = 200, message = "接口返回对象参数")
//    @RequiresPermissions("adminBusiness:adminMemberCard:queryOrderPay")
//    @RequiresPermissionsDesc(menu = {"商家后台", "账户中心-次卡配置"}, button = "查单")
//    @PostMapping("/queryOrderPay")
//    public Result<BankPayQueryVo> queryOrderPay(@RequestBody BankPayQueryDto bankPayQueryDto) {
//        TbBusinessAdminUserPo businessAdminUserPo = getBusinessAdminUser();
//        if (businessAdminUserPo == null) {
//            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
//        }
//
//        Long businessId = businessAdminUserPo.getBusinessId();
//        Long appUserId = businessAdminUserPo.getAppUserId();
//        if (businessId == null || businessId <= 0 || appUserId == null || appUserId <= 0) {
//            Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
//        }
//
//        bankPayQueryDto.setUserId(businessAdminUserPo.getAppUserId());
//        bankPayQueryDto.setBusinessId(businessId);
//
//        return memberCardApi.queryBankPayOrder(bankPayQueryDto);
//    }

}
